package thuyan.dtvt.bkhn;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class WordGuessHelper {

	// khai bao thong tin database
	public static final String DB_NAME = "wordguess.db";
	public static final String TB_NAME = "tblanswer_question";
	public static final int DB_VERSION = 1;
	private SQLiteDatabase database;
	private Context mContext;
	MyOpenHelper helper;

	// xay dung ham khoi tao
	public WordGuessHelper(Context context) {
		this.mContext = context;
		// khoi tao lop de thuc hien viec dong bo hoa csdl
		helper = new MyOpenHelper(mContext);
		database = helper.getWritableDatabase();

	}

	// insert
	public void insertDB(String question, String answer) {
		ContentValues values = new ContentValues();
		values.put("question", question);
		values.put("answer", answer);
		database.insertOrThrow(TB_NAME, null, values);
		// thuc hien viec lay du lieu ra

	}

	// update
	public void updateDB(String question, String answer, int id) {
		ContentValues values = new ContentValues();
		values.put("question", question);
		values.put("answer", answer);
		database.update(TB_NAME, values, "_id" + id, null);
		// thuc hien viec lay du lieu ra

	}

	// select
	public Cursor selectAll() {
		SQLiteDatabase db = helper.getReadableDatabase();
		return db.query(TB_NAME, null, null, null, null, null, null);

	}

	
	//select id
	public Cursor selectId(int id) 
	{
		Cursor cursor;
		cursor = helper.getReadableDatabase().rawQuery("SELECT * FROM tblanswer_question WHERE _ID = "+id, null);
		return cursor;
	}
	//delete
	public Cursor delete()
	{
		Cursor cursor;
		cursor=helper.getReadableDatabase().rawQuery("DELETE FROM tblanswer_question", null);
		return cursor;
	}
	// tao lop noi de thuc hien viec dong bo hoa csdl
	public class MyOpenHelper extends SQLiteOpenHelper {

		public MyOpenHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stubtblanswer_question
			db.execSQL("Create table if not exists tblanswer_question(_id integer primary key autoincrement, question text, answer text)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("drop table if exists tblanswer_question");// xoa bang di
			onCreate(db);// tao bang moi

		}

	}

	

}
